<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input v-model="dataForm.deviceNo" placeholder="设备编号"></el-input>
      </el-form-item>
      <el-form-item label="所属单位" prop="institution">
        <el-input v-model="dataForm.institution" placeholder="所属单位"></el-input>
      </el-form-item>
      <el-form-item label="位置" prop="position">
        <el-input v-model="dataForm.position" placeholder="位置"></el-input>
      </el-form-item>
      <el-form-item label="X坐标" prop="coordinateX">
        <el-input v-model="dataForm.coordinateX" placeholder="X坐标"></el-input>
      </el-form-item>
      <el-form-item label="Y坐标" prop="coordinateY">
        <el-input v-model="dataForm.coordinateY" placeholder="Y坐标"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">开启</el-radio>
          <el-radio :label="0">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <!--
      <el-form-item label="用电参数" prop="parameter">
        <el-input v-model="dataForm.parameter" placeholder="用电参数"></el-input>
      </el-form-item>
      -->
      <el-form-item label="电压(V)" prop="voltage">
        <el-input v-model="dataForm.voltage" placeholder="电压(V)"></el-input>
      </el-form-item>
      <el-form-item label="电流(A)" prop="current">
        <el-input v-model="dataForm.current" placeholder="电流(A)"></el-input>
      </el-form-item>
      <el-form-item label="抄表值(KWH)" prop="meterValue">
        <el-input v-model="dataForm.meterValue" placeholder="抄表值(KWH)"></el-input>
      </el-form-item>
      <el-form-item label="对地电阻(Ω)" prop="rtg">
        <el-input v-model="dataForm.rtg" placeholder="对地电阻(Ω)"></el-input>
      </el-form-item>
      <el-form-item label="漏电流(mA)" prop="leakageCurrent">
        <el-input v-model="dataForm.leakageCurrent" placeholder="漏电流(mA)"></el-input>
      </el-form-item>
      <!--
      <el-form-item label="维修电话" prop="repairPhone">
        <el-input v-model="dataForm.repairPhone" placeholder="维修电话"></el-input>
      </el-form-item>
      -->
    </el-form>

    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import { timestampToDate, dateToTimestamp } from "@/utils/common";
export default {
  data() {
    return {
      visible: false,
      customerCreateTime: "",
      customerDemoCreateTime: "",
      customerActivatedTime: "",
      dataForm: {
        id: "",
        deviceNo: "",
        institution: "",
        position: "",
        coordinateX: "",
        coordinateY: "",
        phone: "",
        voltage: "",
        current: "",
        meterValue: "",
        rtg: "",
        leakageCurrent: "",
        repairPhone: "",
        status: 0
      }
    };
  },
  computed: {
    dataRule() {
      return {
        deviceNo: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        institution: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        position: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        coordinateX: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        coordinateY: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        phone: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        status: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ]
      };
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/business/device/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/business/device/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
